﻿using System;
using ClieOp;
using ClieOp.Record;
using NUnit.Framework;

namespace ClieOpTest
{
    [TestFixture]
    public class RecordTest
    {
        [Test]
        public void BatchsluitrecordTest()
        {
            var record = new Batchsluitrecord {Totaalbedrag = 123456789012345678, Totaalrekeningnummers = 1234567890, Aantalposten = 1234567};
            Assert.AreEqual("9990A12345678901234567812345678901234567" + new string(' ', 10) + "\r\n", record.ToString());
        }

        [Test]
        public void BatchvoorlooprecordTest()
        {
            var record = new Batchvoorlooprecord
                             {Transactiegroep = "A", Rekeningnummeropdrachtgever = 786892692, Batchvolgnummer = 1234, Aanleveringsmuntsoort = "EUR"};
            Assert.AreEqual("0010BA 07868926921234EUR" + new string(' ', 26) + "\r\n", record.ToString());
        }

        [Test]
        public void BestandssluitrecordTest()
        {
            var record = new Bestandssluitrecord();
            Assert.AreEqual("9999A" + new string(' ', 45) + "\r\n", record.ToString());
        }

        [Test]
        public void BestandsvoorlooprecordTest()
        {
            var record = new Bestandsvoorlooprecord
                             {
                                 Aanmaakdatumbestand = Utility.ClieOpDatum(new DateTime(2000, 01, 31)),
                                 Inzendersidentificatie = "AAAAA",
                                 Bestandsidentificatie = "BBBB",
                                 Duplicaatcode = 9
                             };
            Assert.AreEqual("0001A310100CLIEOP03AAAAABBBB9" + new string(' ', 21) + "\r\n", record.ToString());
        }

        [Test]
        public void BetalingskenmerkrecordTest()
        {
            var record = new Betalingskenmerkrecord {Betalingskenmerk = "1"};
            Assert.AreEqual("0150A1" + new string(' ', 15 + 29) + "\r\n", record.ToString());
        }

        [Test]
        public void NaambegunstigderecordTest()
        {
            var record = new Naambegunstigderecord {Naambegunstigde = "abcdefghijklmnopqrstuvwxyzABCDEFGHI"};
            Assert.AreEqual("0170BabcdefghijklmnopqrstuvwxyzABCDEFGHI" + new string(' ', 10) + "\r\n", record.ToString());
        }

        [Test]
        public void NaambetalerrecordTest()
        {
            var record = new Naambetalerrecord {Naambetaler = ""};
            Assert.AreEqual("0110B" + new string(' ', 35 + 10) + "\r\n", record.ToString());
        }

        [Test]
        public void OmschrijvingsrecordTest()
        {
            var record = new Omschrijvingsrecord {Omschrijving = new string('o', 33)};
            Assert.AreEqual("0160A" + new string('o', 32) + new string(' ', 13) + "\r\n", record.ToString());
        }

        [Test]
        public void OpdrachtgeverrecordTest()
        {
            var record = new Opdrachtgeverrecord {Nawcode = 9, Gewensteverwerkingsdatum = 998877, Naamopdrachtgever = "X", Testcode = "Y"};
            Assert.AreEqual("0030B9998877X" + new string(' ', 34) + "Y  " + "\r\n", record.ToString());
        }

        [Test,
         ExpectedException(
             ExpectedMessage =
                 "Error while parsing record line. The line does not start with a recordcode (4 digits). Line value: `AAAAA\r\n'.\r\nParameter name: recordLine"
             )]
        public void ParseFailTest1()
        {
            Record.Parse("AAAAA\r\n");
        }

        [Test, ExpectedException(ExpectedMessage = "Error while parsing record line. Record code 21 does not refer to an existing RecordType.")]
        public void ParseFailTest2()
        {
            Record.Parse("0021A\r\n");
        }

        [Test,
         ExpectedException(
             ExpectedMessage = "Error while parsing field `Recordcode'. This field is read-only and should have value `20', but the parsed value is `21'.")]
        public void ParseFailTest3()
        {
            Record.Parse<Vasteomschrijvingsrecord>("0021A" + new string(' ', 32 + 13) + "\r\n");
        }

        [Test]
        public void ParseTest1()
        {
            var record = Record.Parse<Vasteomschrijvingsrecord>("0020Aabcdefghijklmnopqrstuvwxyz789012" + new string(' ', 13) + "\r\n");
            Assert.AreEqual(20, record.Recordcode);
            Assert.AreEqual("abcdefghijklmnopqrstuvwxyz789012", record.Vasteomschrijving);
        }

        [Test]
        public void ParseTest2()
        {
            var record = (Vasteomschrijvingsrecord)Record.Parse("0020Aabcdefghijklmnopqrstuvwxyz789012" + new string(' ', 13) + "\r\n");
            Assert.AreEqual(20, record.Recordcode);
            Assert.AreEqual("abcdefghijklmnopqrstuvwxyz789012", record.Vasteomschrijving);
        }

        [Test]
        public void TransactierecordTest()
        {
            var record = new Transactierecord {Transactiesoort = "ZZZZ", Bedrag = 0, Rekeningnummerbetaler = 123456789, Rekeningnummerbegunstigde = 3213213213};
            Assert.AreEqual("0100AZZZZ00000000000001234567893213213213" + new string(' ', 9) + "\r\n", record.ToString());
        }

        [Test]
        public void VasteomschrijvingsrecordTest()
        {
            var record = new Vasteomschrijvingsrecord {Vasteomschrijving = ""};
            Assert.AreEqual("0020A" + new string(' ', 32 + 13) + "\r\n", record.ToString());
        }
    }
}